import { message } from 'antd';
export default {
  namespace: 'ds',

  state: {
    offlineZmcStates: {}, // 离线上料的状态
    camZmcStates: {}, // 上料控制器状态
    cbjZmcStates: {}, // 裁切机控制器状态
    cutterZmcStates: {}, // 铡刀控制状态
    cameraGroupStates: {}, // 相机组控制器状态
    autoState: false, // 后台自动作业是否停止
    mainAlarmInfo: '', // 铡刀总控制器的报警信息框
    offlineAlarmInfo: '', // 离线上料的报警信息框
    mainAlarmInfoVisible: false, // 底层报警提示框是否显示
    offlineAlarmInfoVisible: false, // 你先上料的提示框是否显示
    offlineOnlineStart: false,  // 离线在线状态
    shieldStart: [],
  },

  // 和后台交互
  effects: {
    *handleDeviceStatesMessage({ payload }, { call, put }) {
      const { msg } = payload;
      yield put({
        type: 'updateDeviceStates',
        payload: msg,
      });
    },
    *offlineStart({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_start_offline_zmc',
        []
      );
      if (response === 'ok') {
        message.success('离线上料启动成功!', 3);
      } else {
        message.error('离线上料启动失败!' + response, 5);
      }
    },
    *offlineStop({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_stop_offline_zmc',
        []
      );
      if (response === 'ok') {
        message.success('离线上料停止成功!', 3);
      } else {
        message.error('离线上料停止失败!' + response, 5);
      }
    },
    *offlineReset({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_reset_offline_zmc',
        []
      );
      if (response === 'ok') {
        message.success('离线上料复位成功!', 3);
      } else {
        message.error('离线上料复位失败!' + response, 5);
      }
    },
    *offlineSwitchThrowBoard({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_offline_zmc_switch_throw_board',
        []
      );
      if (response === 'ok') {
        message.success('切换成功!', 3);
      } else {
        message.error('切换失败!' + response, 5);
      }
    },
    *offlineAllowCatchBoard({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_offline_zmc_allow_catch_board',
        []
      );
      if (response === 'ok') {
        message.success('设置成功!', 3);
      } else {
        message.error('设置失败!' + response, 5);
      }
    },
    *offlineAllowTakeBoard({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_offline_zmc_allow_take_board',
        []
      );
      if (response === 'ok') {
        message.success('设置成功!', 3);
      } else {
        message.error('设置失败!' + response, 5);
      }
    },
    *offlinePutBoardFinish({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_offline_zmc_put_board_finish',
        []
      );
      if (response === 'ok') {
        message.success('设置成功!', 3);
      } else {
        message.error('设置失败!' + response, 5);
      }
    },
    *offlineHandPutBoardConfirm({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_offline_zmc_hand_put_board_confirm',
        []
      );
      if (response === 'ok') {
        message.success('设置成功!', 3);
      } else {
        message.error('设置失败!' + response, 5);
      }
    },
    *offlineCarIsInPlace({ payload }, { call, put }) {
      const response = yield call(
        window.eel.run_py,
        'offlineZmc',
        'web_request_offline_zmc_car_is_in_place',
        []
      );
      if (response === 'ok') {
        message.success('设置成功!', 3);
      } else {
        message.error('设置失败!' + response, 5);
      }
    },
  },

  // 状态更新
  reducers: {
    updateDeviceStates(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

    closeMainAlarmInfoModal(state, action) {
      return {
        ...state,
        mainAlarmInfoVisible: false,
        mainAlarmInfo: '',
      };
    },
  },
};
